package service

import (
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/zhang201702/zhang/zlog"
	"usad2/app/dao"
)

type Service interface {
	Save(data g.Map) (result interface{}, err error)
	Insert(data g.Map) (result interface{}, err error)
	Update(data g.Map, id string) (result interface{}, err error)
	Delete(id string) (result interface{}, err error)
	DeleteAll(data g.Map) (result interface{}, err error)
	List(data g.Map) (list g.List, err error)
	Fetch(id string) (result g.Map, err error)
}

type BaseService struct {
	Name string
	Dao  dao.Dao
}

func (service *BaseService) Save(data g.Map) (result interface{}, err error) {
	result, err = service.Dao.Save(data)
	if err == nil {
		result = data
	}
	return result, err
}
func (service *BaseService) Insert(data g.Map) (result interface{}, err error) {
	return service.Dao.Insert(data)
}
func (service *BaseService) Update(data g.Map, id string) (result interface{}, err error) {
	return service.Dao.Update(data, id)
}
func (service *BaseService) Delete(id string) (result interface{}, err error) {
	return service.Dao.Delete(id)
}
func (service *BaseService) DeleteAll(data g.Map) (result interface{}, err error) {
	return service.Dao.DeleteAll(data)
}
func (service *BaseService) List(data g.Map) (list g.List, err error) {
	return service.Dao.List(data)
}
func (service *BaseService) Fetch(id string) (result g.Map, err error) {
	return service.Dao.Fetch(id)
}

func (service *BaseService) toMap(obj interface{}) g.Map {
	json, err := gjson.DecodeToJson(obj)
	var result g.Map
	if err != nil {
		zlog.Error(err, "toList,异常")
		return g.Map{}
	}
	result = json.ToMap()
	return result
}

func (service *BaseService) toList(obj interface{}) g.List {
	json, err := gjson.DecodeToJson(obj)
	result := g.List{}
	if err != nil {
		zlog.Error(err, "toList,异常")
		return result
	}
	tempList := json.ToArray()
	for i := range tempList {
		result = append(result, tempList[i].(map[string]interface{}))
	}
	return result
}
func (service *BaseService) toMapString(obj interface{}) (str string) {
	json := gjson.New(obj)
	str, err := json.ToJsonString()

	if err != nil {
		zlog.Error(err, "toMapString，异常")
		str = "{}"
	}
	if str == "null" {
		str = "{}"
	}
	return str
}

func (service *BaseService) toListString(obj interface{}) (str string) {
	json := gjson.New(obj)
	str, err := json.ToJsonString()
	if err != nil {
		zlog.Error(err, "toListString，异常")
		str = "[]"
	}
	if str == "null" {
		str = "[]"
	}
	return str
}

func (service *BaseService) copyData(data g.Map) g.Map {
	result := g.Map{}
	for k := range data {
		v := data[k]
		switch v.(type) {
		case map[string]interface{}:
			result[k] = service.copyData(v.(map[string]interface{}))
		case []map[string]interface{}:
			temps := v.([]map[string]interface{})
			vv := make([]map[string]interface{}, 0)
			for i := range temps {
				vv = append(vv, service.copyData(temps[i]))
			}
			result[k] = vv
		default:
			result[k] = v
		}
	}
	return result
}
